clear all

global replica_dir="<add path to your working directory>"
global data_dir="$replica_dir/data"
global output_dir="$replica_dir/output"

global simce_data_dir="<add path to your SIMCE data directory>"

*------------------------------------------------------------------------------*

* SIMCE 2011

*------------------------------------------------------------------------------*

**# Student scores

/*
	Note:
	
	idalumno: 	unique identifier for "mrun-letra_curso-cod_curso" record  
				important when linking it to teacher survey piece, use together with letra_curso-cod_curso
	
	mrun: unique student identifier to link data across years
	
------------------------------------------------------------------------------*/
 
 
use "$simce_data_dir/8B/simce8b2011/Archivos DTA (Stata)/simce8b2011_alu_publica_final.dta", clear

keep if noptje_lect8b_alu==0 | noptje_mate8b_alu==0 | noptje_nat8b_alu==0 // con puntaje

duplicates report mrun
duplicates examples mrun
duplicates tag mrun, g(ndupobs)
//browse if ndupobs>0
unique mrun

keep if ndupobs==0

rename  (ptje_lect8b_alu 	ptje_mate8b_alu 	ptje_nat8b_alu 		ptje_soc8b_alu ) /// 
		(lect_post			mate_post			nat_post			soc_post)


keep idalumno mrun rbd lect_post mate_post nat_post  //  noptje_lect8b_alu noptje_mate8b_alu noptje_nat8b_alu

save "$data_dir/simce_b8_2011.dta", replace


*------------------------------------------------------------------------------*

**#  School survey

*------------------------------------------------------------------------------*

use "$simce_data_dir/8B/simce8b2011/Archivos DTA (Stata)/simce8b2011_rbd_publica_final.dta", clear

egen sch_stno_post = rowmax(nalu_8b_rbd nalu_lect8b_rbd nalu_mate8b_rbd nalu_soc8b_rbd nalu_nat8b_rbd)

rename  (cod_depe1 		cod_grupo 		cod_reg_rbd) /// 
		(sch_depe1_post	sch_gse_post	cod_reg_rbd_post)

keep  rbd sch_stno_post sch_depe1_post sch_gse_post cod_reg_rbd_post

merge 1:m rbd using "$data_dir/simce_b8_2011.dta"

//browse if _merge==1
keep if _merge==3
drop _merge

sort mrun

rename rbd rbd_post

save "$data_dir/simce_b8_2011.dta", replace

*------------------------------------------------------------------------------*




 
